<style scoped lang="scss">

</style>

<template>
  <div>

    <el-form :inline="true" :model="queryParams" class="demo-form-inline">
      <el-form-item label="xxx : ">
        <el-input v-model="queryParams.xxx" placeholder="xxx"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="query">查询</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" @click="handleAdd" plain icon="el-icon-plus" size="mini">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" @click="handleDelete()" plain icon="el-icon-delete" size="mini">删除</el-button>
      </el-col>
    </el-row>

    <br>

    <el-table :data="tableData" v-loading="loading" stripe border @selection-change="handleSelectionChange">

      <el-table-column
          type="selection"
          width="55">
      </el-table-column>

      <el-table-column prop="xxx" label="xxx">
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleEdit(scope.row.xxxId)" plain icon="el-icon-edit" size="mini">
            编辑
          </el-button>
          <el-button type="danger" @click="handleDelete(scope.row.xxxId)" plain icon="el-icon-delete"
                     size="mini">删除
          </el-button>
        </template>
      </el-table-column>

    </el-table>

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryParams.pageNum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <el-dialog :title="title" :visible.sync="dialogVisible" width="30%" append-to-body>

      <el-form ref="dialogForm" :model="dialogForm" :rules="dialogRules" lable-width="80px">

        <el-form-item prop="nnn" label="lll : ">
          <el-input v-model="dialogForm.xxx" placeholder="ppp"></el-input>
        </el-form-item>

      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </span>

    </el-dialog>

  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "template",
  components: {
    axios
  },
  created() {
    this.getList()
  },
  data() {
    return {
      circleUrl: "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png",
      tableData: [],
      selectedIds: [],
      title: "",
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        xxx: ""
      },

      dialogForm: {
        xxx: null,
      },
      dialogRules: {
        xxx: [
          {required: true, message: '请输入xxx', trigger: 'blur'},
          {min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur'}
        ]
      },
      loading: false,
      dialogVisible: false
    }
  },
  methods: {
    query() {
      this.pageNum = 1
      console.log("queryParams", this.queryParams)
      this.getList()
    },
    resetDialogForm() {
      this.dialogForm = {
        xxx: null,
      }
    },
    handleSizeChange(pageSize) {
      console.log("pageSize", pageSize)
      this.queryParams.pageSize = pageSize
      this.getList()
    },
    handleCurrentChange(pageNum) {
      console.log("pageNum", pageNum)
      this.queryParams.pageNum = pageNum
      this.getList()
    },
    dateFormat(row, column, cellValue, index) {
      return cellValue ? cellValue.substring(0, 10) : cellValue
    },
    infoFormat(row, column, cellValue, index) {
      return cellValue.length > 10 ? cellValue.substring(0, 10) + "..." : cellValue
    },
    getList() {
      this.loading = true
      axios({
        method: 'get',
        url: 'http://localhost:9090/xxx/page',
        params: this.queryParams
      }).then(res => {
        this.tableData = res.data.rows
        this.total = res.data.total
        this.loading = false
      })
    },
    onSubmit() {
      console.log('submit!')
      console.log(this.dialogForm)
      this.$refs["dialogForm"].validate((valid) => {
        if (valid) {

          this.loading = true
          let method = this.title == "添加" ? "post" : "put"

          axios({
            method: method,
            url: 'http://localhost:9090/xxx',
            data: this.dialogForm
          }).then(res => {
            if (res.data.code != 200) {
              this.$message({
                message: '添加失败',
                type: 'warning'
              });
              return
            }
            this.$message({
              message: '添加成功',
              type: 'success'
            });
            this.dialogVisible = false
            this.loading = false
            this.getList()

          })
        } else {
          console.log('error submit!!');
          return false;
        }
      })

    },
    handleAdd() {
      this.resetDialogForm()
      this.title = "添加"
      this.dialogVisible = true
    },
    handleEdit(id) {
      this.loading = true
      axios({
        method: 'get',
        url: 'http://localhost:9090/xxx/' + id
      }).then(res => {
        this.dialogForm = res.data.data
        this.title = "修改"
        this.loading = false
        this.dialogVisible = true
      })
    },
    handleDelete(id) {
      let ids = id || this.selectedIds.join(",")
      axios({
        method: 'delete',
        url: 'http://localhost:9090/xxx/' + ids,
      }).then(res => {
        console.log(res);
        if (res.data.code != 200) {
          this.$message({
            message: '删除失败',
            type: 'warning'
          });
          return
        }
        this.$message({
          message: '删除成功',
          type: 'success'
        });
        this.getList()
      }).catch(err => {
        this.$message({
          message: '删除异常',
          type: 'error'
        });
      })
    },
    handleSelectionChange(val) {
      this.selectedIds = val.map(item => item.xxxId)
      console.log(this.selectedIds)
    }

  }
}
</script>